package com.demo.config.security;

import com.demo.common.JsonResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author lc
 * @since 2022/6/1
 */
@Configuration
public class SecurityFilter implements Filter {

    //过滤连设置属性是否存在
    @Override
    public void doFilter(ServletRequest rt, ServletResponse re, FilterChain v) throws IOException, ServletException {
        System.out.println(rt.getAttribute("Token"));
        String token = rt.getParameter("Token");

        if (token == null) {
            re.setContentType("text/json;charset=UTF-8");
            re.getWriter().write(new ObjectMapper().writeValueAsString(JsonResult.fail("认证失败")));
            return;
        }
        v.doFilter(rt, re);
    }
}
